package client.ui.view.module;

import client.assets.Icons;
import client.control.Controller;
import client.control.ResponseEntity;
import client.entity.Comment;
import client.entity.User;
import client.ui.component.divider.Divider;
import client.ui.theme.ThisFont;
import client.ui.util.MaterialColors;
import client.util.ImageUtil;
import com.alibaba.fastjson.JSONObject;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
  * @Description 一条评论
  * @author 孔明浩
  * @Date 2020/3/31
  */
public class CommentItem extends JPanel {
    private JLabel headImageLabel;
    private JLabel usernameLabel;
    private Comment comment;
    public CommentItem(Comment comment) {
        this.comment = comment;
        initial();
        //获取用户名
        Executor singleThreadExecutor = Executors.newSingleThreadExecutor();
        singleThreadExecutor.execute(new Runnable() {
            @Override
            public void run() {
                ResponseEntity responseEntity = Controller.USER_CONTROL.getOtherUserInfo(comment.getUserId());
                if (responseEntity.getCode() == 0){
                    User user = JSONObject.parseObject(responseEntity.getData(), User.class);
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            usernameLabel.setText(user.getUserName());
                        }
                    });
                }
            }
        });
        //获取头像
        singleThreadExecutor.execute(new Runnable() {
            @Override
            public void run() {
                ImageIcon headIcon = Controller.USER_CONTROL.getHeadImage(comment.getUserId(), 30);
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        headImageLabel.setIcon(headIcon);
                    }
                });
            }
        });
    }

    protected void initial(){
        this.setLayout(new BorderLayout());
        this.setBackground(MaterialColors.WHITE);

        Box user = Box.createHorizontalBox();
        usernameLabel = new JLabel(comment.getUserId() + "");
        usernameLabel.setFont(ThisFont.MIDDLE_FONT);
        headImageLabel = new JLabel(ImageUtil.getImageIcon(Icons.head_image,"",28,28),
                SwingConstants.LEFT);
        headImageLabel.setMaximumSize(new Dimension(30,30));
        headImageLabel.setBorder(new EmptyBorder(0,4,0,4));
        user.add(headImageLabel);
        user.add(usernameLabel);

        JLabel content = new JLabel(comment.getMessage());
        content.setBorder(new EmptyBorder(0,38,0,38));
        content.setFont(ThisFont.SMALL_FONT);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        JLabel date = new JLabel(simpleDateFormat.format(comment.getDate()));
        date.setBorder(new EmptyBorder(4,38,0,38));
        date.setFont(ThisFont.SMALL_FONT);
        date.setForeground(MaterialColors.GRAY_600);

        this.add(user, BorderLayout.NORTH);
        Box vBox = Box.createVerticalBox();
        vBox.add(content);
        vBox.add(date);
        this.add(vBox, BorderLayout.CENTER);
        this.add(new Divider(), BorderLayout.SOUTH);
    }
}
